import 'package:flutter/material.dart';
import 'package:jd_mall_flutter/pages/home/index.dart';
import 'package:jd_mall_flutter/pages/category/index.dart';
import 'package:jd_mall_flutter/pages/cart/index.dart';
import 'package:jd_mall_flutter/pages/profile/index.dart';

class Layout extends StatefulWidget {
  const Layout({super.key});

  @override
  State<Layout> createState() => _LayoutState();
}

class _LayoutState extends State<Layout> {
  int currentIndex = 0; // 记录当前索引

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: IndexedStack(
          index: currentIndex,
          children: const [HomeIndex(), CategoryIndex(),CartIndex(),ProfileIndex()],
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        showSelectedLabels: false, // 隐藏选中标签
        showUnselectedLabels: false, // 隐藏未选中标签
        type: BottomNavigationBarType.fixed,
        items: barItemList(),
        currentIndex: currentIndex,
        selectedItemColor: Colors.red,
        unselectedItemColor: Colors.black,
        onTap: (index) {
          setState(() {
            currentIndex = index;
          });
        },
      ),
    );
  }

  List<BottomNavigationBarItem> barItemList() {
    List<BottomNavigationBarItem> items = [];
    items.add(
      BottomNavigationBarItem(
        icon: Image.network(
          'https://img11.360buyimg.com/jdphoto/s130x100_jfs/t1/81741/30/12345/4140/5d9c4b13E726f0a1e/82c582e7c375e4b3.png',
          width: 70,
          height: 70,
        ),
        activeIcon: Image.network(
          'https://img11.360buyimg.com/jdphoto/s130x100_jfs/t1/67550/26/12426/5094/5d9c4b13Eea435a3f/81328b0609c60a3c.png',
          width: 70,
          height: 70,
        ),
        label: '首页',
      ),
    );
    items.add(
      BottomNavigationBarItem(
        icon: Image.network(
          'https://img11.360buyimg.com/jdphoto/s130x100_jfs/t1/56507/6/12787/3168/5d9c4b12Ef363dd8d/4af32f42575509d8.png',
          width: 70,
          height: 70,
        ),
        activeIcon: Image.network(
          'https://img11.360buyimg.com/jdphoto/s130x100_jfs/t1/48787/24/12910/3390/5d9c4b12Ee63270e4/4481f5b3dbad979d.png',
          width: 70,
          height: 70,
        ),
        label: '分类',
      ),
    );
    items.add(
      BottomNavigationBarItem(
        icon: Image.network(
          'https://img11.360buyimg.com/jdphoto/s130x100_jfs/t1/64954/4/12406/3529/5d9c4b12Ee7a82735/f2fe0a88bf344736.png',
          width: 70,
          height: 70,
        ),
        activeIcon: Image.network(
          'https://img11.360buyimg.com/jdphoto/s130x100_jfs/t1/45205/32/12889/3890/5d9c4b12E7bc2c425/dd60fafbde8542ca.png',
          width: 70,
          height: 70,
        ),
        label: '购物车',
      ),
    );
    items.add(
      BottomNavigationBarItem(
        icon: Image.network(
          'https://img12.360buyimg.com/img/s130x100_jfs/t1/203343/14/10880/3097/61641568E184a8e0b/4522f6ec5bb9b10b.png',
          width: 70,
          height: 70,
        ),
        activeIcon: Image.network(
          'https://img11.360buyimg.com/jdphoto/s130x100_jfs/t1/47480/36/12929/3209/5d9c4b13E97caa63a/4dc0ec8a7e47c2b7.png',
          width: 70,
          height: 70,
        ),
        label: '我的',
      ),
    );
    return items;
  }
}